package work20241226;

/**
 * @author zdl
 * @date 2024/12/26
 */
class ThreadZhu implements Runnable {
    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            System.out.println("我是主线程:"+i);
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
}

class ThreadHelp implements Runnable {
    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            System.out.println("我是守护线程:" + i);
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }


    }
}

public class Demo6 {
    public static void main(String[] args) {
        //6. 创建一个守护线程，该线程每隔100毫秒输出一次“我是守护线程”，主线程结束后，守护线程也随之结束。
        Thread thread1 = new Thread(new ThreadZhu(), "主线程");
        Thread thread2 = new Thread(new ThreadHelp(), "守护线程");
        thread2.setDaemon(true); //thread2为守护线程，主线程结束，守护线程跟着结束
        thread1.start();
        thread2.start();
    }
}
